﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace AcctrueWMS.Foundation.WebControls
{
    public class AccRadioButton : AccControl
    {
        public override string Render()
        {
            StringBuilder sb = new StringBuilder();
            #region 生成控件
            sb.Append(GetOptions());
            if (this.Musted)
            {
                sb.Append("<font color=\"red\">*</font>");
            }
            #endregion
            return sb.ToString();
        }

        private string GetOptions()
        {
            StringBuilder sb = new StringBuilder();
            if (base.DataSource is DataTable)
            {
                DataTable dtOptions = base.DataSource as DataTable;
                int rowIndex = 0;
                foreach (DataRow row in dtOptions.Rows)
                {
                    rowIndex++;
                //    sb.AppendFormat("<nobr><input type=radio id='{4}' name={0} value='{1}' musted='{2}' ctitle='{5}' />{3}</input></nobr>", base.ID, row[base.KeyMember].ToString(), base.Musted.ToString().ToLower(), row[base.ValueMember].ToString(),this.ID+"$"+rowIndex.ToString(),base.Title);

                   // 这里有问题需要改进  暂用上
                    sb.AppendFormat("<nobr><input type=radio name={0} value='{1}' {3} {4} {5} {6}/>"
                            + "{2}</input></nobr>", new object[] {
                        base.ID,
                        row[base.KeyMember],
                        row[base.ValueMember],
                        row[base.KeyMember].ToString() ==GetControlValue() ? "checked=\"checked\"" : "",
                        base.Enabled ? string.Empty : "readonly='readonly' style='background-color:#F7F8F0'  ",
                        this.Visible?string.Empty:" style='display:none;font-size:	9pt;width:100%;height:20px;border:1px solid #7b9ebd;'",
                        Property
                    });
                }
            }
            else if (base.DataSource is Dictionary<string, string>)
            {
                Dictionary<string, string> dic = base.DataSource as Dictionary<string, string>;
                int rowIndex = 0;
                foreach (KeyValuePair<string, string> kev in dic)
                {
                    rowIndex++;
                 //   sb.AppendFormat("<nobr><input type=radio id='{4}' name={0} value='{1}' musted='{2}' ctitle='{5}' />{3}</input></nobr>", base.ID, kev.Key.ToString(), base.Musted.ToString().ToLower(), kev.Value.ToString(), kev.Key.ToString() + "$" + rowIndex,base.Title);

                  //  暂用上
                    sb.AppendFormat("<nobr><input  type=radio name={0} value='{1}' {3} {4} {5} {6}>"
                                         + "{2}</input></nobr>", new object[] {
                    base.ID,
                    kev.Value ,          
                    kev.Key,
                    kev.Key == GetControlValue() ? "checked=\"checked\"" : "",
                    base.Enabled ? string.Empty : "disabled",
                    this.Visible?string.Empty:" style='display:none'",
                    Property
                        });
                }

            }

            return sb.ToString();
        }

    }
}
